Hey there.
Welcome back.
Now that we have everything we need set up on the client, we can get started scripting our story.
You'll want to open up the game service and the game sections module scripts.
And inside of the game service is where we're going to have to fill out the functionality for when our
inside part gets touched.
Before we do that, we need to create two booleans real quick, and these booleans are going to keep
track of when our inside is touched.
So this is going to act as a debounce to make sure that it only gets fired once, or the function gets
executed once.
And then we'll have another, uh, variable to tell us whether or not we're ready for section number
two.
So that way if the basement part gets touched, it's going to ignore it until we are ready to start
the second section of our game.
Now, what we want to do when the inside part is touched is we first want to confirm that a player's
character touched it.
So we'll get the other part that touched this inside part and if not other part.
Dot parent find first child which is a humanoid.
Then we're just going to return because a player's character did not touch this.
We're also going to use the player service to see if we actually did get a player that touched it using
the player's.
Get player from character function and pass the other part.
Dot parent.
If there is no player, then we're going to return as well.
And then the last thing we need to check is whether or not the inside was touched.
So if inside touched.
Is true, then we need to return.
Otherwise we're going to set inside touch equal to true.
And then this is where we use our sections module script and execute a function to do all of the functionality
for the first section part of our story.
So inside of game sections we can create a public function to start the first section.
So it could be game sections.
Start section one.
And this is going to be the function for handling all that kind of good stuff in our story.
And we can call that function from right here.
And then after all that functionality is done in our start section one function, then we can set the
ready for section two boolean equal to true.
To let this function right here know that it's ready to start.
You know the next part of our story.
So inside of our game sections module script we're going to need four services.
One is going to be the server script service.
The next service we'll need is server storage.
We're going to need replicated storage.
And then we're also going to need the team service.
From this point, we can start making references to a whole bunch of different events.
One is going to be, for example, our door event which is in server storage, dot events, dot Bindable
dot door.
We want to make a reference to the game comms event for communicating with the client.
So replicated storage, dot events, dot remotes, dot game communication.
And that means we're going to have to get the game comms enum.
And this one we're going to label.
As for the client.
And we can require for replicated storage dot modules dot enums dot game communication enum.
And then we're also going to get the game comms event for the server or the Bindable event.
So we could call this game comms a Bindable.
Which is equal to server storage, dot events, dot bindable dot game comms.
And then we're also going to get the game comms enum for our Bindable event.
And we can require it from server script service dot server, dot modules, dot enums dot game communication
enum.
We'll also want to make a reference to our message guy event and replicated storage dot events dot remotes
dot message guy.
We'll want to make a reference to our update Guy event and replicated storage, dot events, dot remotes,
dot update UI, and then we also want to get the GUI actions enum require it from replicated storage,
dot modules, dot enums, dot gui actions enum.
We're also going to want our manipulate camera event which is in replicated storage, dot events, dot
remotes, dot manipulate camera.
And we're also going to get the camera manipulation enum.
And we can require that from replicated storage dot modules, dot enums, dot camera manipulation enum.
I'm also going to make a reference to a folder in the workspace called Camera Parts.
So workspace dot filtering folder dot camera parts.
And this folder contains all of the invisible parts that we want to set our camera to.
So for example here we want to set our camera to be facing towards Squidward.
And then in the next section of our story, we'll have the players camera facing down the stairs.
Here I'm also going to be creating a table.
I'm going to call it images, and this is going to be a table to store all of the different, uh, IDs
for images we need to set for when we have to give players a message to display.
So remember that when we want to display that message UI on the player screen, we need the image of
the person who is speaking the name and the message that they want to display.
So for this one, we can have an image of our Squidward friend, and I'll just call this Squidward.
And the ID for this image is going to be an RGB asset ID, colon slash slash.
And the number for this is (870) 193-7876.
So this is going to be our Squidward image.
And then we also want to make a table for containing all the messages.
So for the introductory messages we can call this Squidward one.
So this will be the first set of messages, and it's going to be an array containing a whole bunch of
different strings.
So the first message could be telling the player once they enter the restaurant, we can tell them,
well, you're late.
Not surprising.
And then we can give them a second message.
We could say, you've got a lot of work to do before we open in the morning.
So that alludes to them having to do some tasks.
And then we could tell them that Mr. Krabs wants all the tables cleaned.
We want the floors mopped, we need the toilets scrubbed, and we need the kitchen restocked.
And then the last message we could give for this introduction scene is we could say, sadly, I have
to sit here and supervise you.
So hurry up and get your work done so we can leave.
So this is just our good Squidward friend being lazy here.
So this will be the first set of messages that we can give to the player.
Last but not least, though, the last variable I am going to need is a random data type.
So random dot new.
Inside of our objective section here in the Game sections module script, we're also going to add a
new objective which will label as tasks.
And the text for this one can be something like complete your tasks.
So that would be like cleaning the tables, cleaning the toilets, stuff like that.
And we could also make a variable to keep track of whether or not this was fulfilled.
And then some private functions we're going to need for now.
One is going to be teleporting a player around a particular keyframe.
So if you remember on the client, when the players got off the bus, we teleported them around a part.
Well, now we want to create a dedicated function here on the server for doing the exact same thing.
So this function will allow us to teleport a player, and we can call it around a keyframe.
And we'll get past a player instance here, as well as the keyframe that we want to teleport the player
around.
We're also going to want to create a function for calculating the weight or yield duration for some
text.
What the heck is this going to do?
Well, when we give the players the messages that they need to display on the screen, all that stuff
is going to be happening on the client side, right?
The server isn't going to know exactly how long to wait before starting the next section of the story,
until after the players have that text or the message UI on their screen removed, so it can create
a function here.
Calculate.
To basically take one of these tables, we'll get the table, and then we'll also pass the time in between
these different messages, as well as the time in between displaying the different characters.
And then we can have the server calculate how long to wait that matches how long it would take to display
all of these messages on the player's screen.
So this function would get passed a table containing all the messages, the time in between displaying
the messages, and the time in between displaying each of the characters.
And what we could do here is we could create a variable called wait duration, and that will be equal
to the number of messages inside of our message table.
Let me rename that to message table.
And we just multiply that by the time in between the different messages.
After that, we'll create a variable and we'll call it total characters equal to zero for now.
And then what we need to do is we need to loop through every single message that is inside of the message
table.
And we need to set total characters plus equal to the length of this particular message here.
Once we get the total amount of characters that need to be displayed on the screen, we can set the
wait duration equal to itself, plus the total characters multiplied by the time that we need to wait
in between different characters.
And then after that, we can just return the wait duration.
So this will give us a rough estimate of how long the server needs to wait after it tells the players
to, you know, display this set of messages on their screens.
We could also fill out this function real quick I guess as well.
For this one, all we need to do is get the player dot character dot humanoid root part and set the
key frame equal to the key frame that was passed to this function.
And then we just add a vector three offset dot new and we can move it around the key frame anywhere
-4 to 4 studs on the x axis.
We'll do zero on the y axis.
And then basically the same thing on the z axis okay.
So when our start section one function gets called what we could do is we could put a Warn statement
in here and print in the console that we are starting section one of the game.
And remember, this function gets called when the player or one single player touches this inside part.
So when a player touches this inside part, we want to teleport all the other players in the game inside
of our lobby.
And we also want to shut and lock the main door.
So what we could do is we could first use our game comms event and fire to all of the clients using
the action of game comms.
Enum client two client dot change sound to inside.
Because remember, not every single player will have the opportunity to touch this part, but since
we're moving them all inside, we still want to have them muffle any outdoor sounds.
So we'll make sure to tell them to do that.
And then after that, we're going to loop through every single player that's in the alive team.
So teams dot alive get players.
And we need to teleport this player around the C frame that is inside of the workspace filtering folder.
Dot teleport parts dot inside dot C frame.
And that's going to be this part right here.
This is our inside part that we want to teleport the players around.
After we do that, we can go ahead and shut the main door.
So we'll use the door event and fire and give the action that we want to close a door.
And the door model that we need to close is going to be in the workspace dot Krusty Krab Dot doors,
and that's going to be the main entrance door.
And then after we close it, we also need to lock the door to prevent any players from opening it as
well, so we can give the action of lock door.
Once all the players are inside, we can set the game sections dot objectives, dot enter dot fulfilled
equal to true and then we're going to use the update guy event and fire to all the clients in our game
using the GUI actions enum dot objective fulfilled to tell them that they fulfilled the objective which
was entering inside of the restaurant.
So the fulfilled objective name is equal to enter.
And then we also can use the manipulate cam, event and fire again to all of the clients in the game
that we want to manipulate their camera.
So two clients move camera two, and we want them to move their camera to this invisible part right
here, which will be facing our Squidward friend.
And if you remember what we need to pass, here is a table which will be our args table.
And one key we need to have in here is to represent the tween data for tweening the camera.
So we want the tween to last one second.
And we could use an enum easing style of quad just to be a little fancy.
And then we also need to pass the goal for the camera.
So the camera goal is going to be equal to.
We want to change the C frame of our camera.
And the C frame needs to be equal to inside of our camera parts folder.
We'll get the facing Squidward part, and we're going to pass that C frame.
So now, because we have told all of the clients to do this specific action, all of their cameras are
now going to be stuck facing Squidward.
And now, since all their cameras are stuck facing Squidward, we can go ahead and start displaying
messages on the screen.
Before we do that, we need to calculate how long the server needs to wait for whatever set of messages
we're passing.
So we'll create a variable called wait duration, and we're going to use our calculate wait duration
for text function.
And we want to pass the messages dot Squidward won array.
So we're going to be displaying all those messages for the time.
In between messages we could do like three seconds.
We want to wait.
And then for the time in between different characters, we're going to do something like point.
Uh oh two.
And then we can use our message guy event and fire to all the clients in our game.
That we need them to display some messages on the screen.
So the person who is going to be speaking is Squidward.
So this needs to be a string.
We'll just call it Squidward.
We need to pass the image that represents Squidward, which is inside of our images table dot Squidward.
So we pass that ID and then we need to pass the message table, which will be equal to messages dot
Squidward one.
And then we also need to pass the delay between the different characters we need to display, which
is going to be 0.02.
And then the delay between messages is also going to be three seconds.
And then if the camera is manipulated.
For our message, we're going to set that equal to true as well, to let them all know that, hey,
the camera was manipulated for this message.
So once you're done displaying the messages on your screen, you want to move the camera back to the
player.
Because as you remember, the camera is currently stuck to the C frame of this part.
And actually let me get rid of these extra, uh, characters there.
Okay, so after we tell all the players to start displaying a message on the screen, the server needs
to, of course, yield.
So we're going to use the task dot wave function and use our wait duration.
And since there might be a little bit of delay between when this information gets passed to all of our
clients and stuff like that, I'm going to add an extra five seconds to our wait duration just in case.
You know, it's never bad to be safe than sorry.
And then after we wait those five seconds, we need to use our update guy event and fire to all of the
clients and tell them that they have a new objective, which is to complete all of their tasks so we
could do guy actions.
Enum dot add objective.
And remember, the key we need to pass here is we need a name for this objective, which is going to
be tasks.
And the text for this objective is going to be inside of our sections.
So game sections dot objectives dot tasks, dot text.
And then once we give them this new objective that they need to complete the tasks, we actually need
to go ahead and, you know, set up all the tasks on the server such as mopping the floor, cleaning
the tables, things like that.
So that's what we're going to do in the next lecture.
But before we leave, let's go ahead and test out the current code that we have inside of our start.
Section one function.
So up here we can go ahead and hit play.
And let's see how our story is working out so far.
So when I go to touch this invisible part that's right in front of us, we should get teleported inside.
Our camera should get set to be looking at Squidward.
This main door should close, and then we should get the message UI displaying the messages on our screen.
So let's go ahead and see what happens.
And there we go.
Our camera moves to Squidward, and we're having the typewriting effect of our text appear on the screen.
Here he is.
And here's his name.
And there we go.
Our players camera gets set back to our, um, character.
And now we get a new objective of we need to complete our tasks.
So next up we're going to be scripting the, uh, module script responsible for setting up all of the
tasks in our game.
See you there.